package cn.wygandwdn.core.hook;

import cn.wygandwdn.core.config.Config;
import cn.wygandwdn.core.registry.ServiceRegistry;
import cn.wygandwdn.core.registry.nacos.NacosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:
 * @author: WYG
 * @time: 2021/10/31 19:38
 */

public class ShutdownHook {
    private static final Logger log = LoggerFactory.getLogger(ShutdownHook.class);

    private final ServiceRegistry registry = Config.getRegistry();
    private static final ShutdownHook shutdownHook = new ShutdownHook();

    public static ShutdownHook getShutdownHook() {
        return shutdownHook;
    }

    public void addClearHook(String host, int port) {
        log.debug("应用关闭后自动注销所有已注册服务");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.debug("注销所有已注册服务");
            registry.clearRegistry(host, port);
        }));
    }
}
